//
//  ZyaSiliaoContentVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/3/24.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
 
import Eureka
class ZyaSiliaoContentVC: KSFormViewController {
    
    var model = ZyaFeedOrderList(JSON:["":""])!
    var details = [ZyaPurchaseDetail]()
    var detailSection:Section = {
        let sec = Section() {
            $0.header = sectionHeaderFooterForSpace()
            $0.footer = sectionHeaderFooterForSpace()
        }
        return sec
    }()
    var btn:GSCaptchaButton?
    lazy var tbinfoView: ZyaTbInfoView = {
        
        var tbinfoView = (Bundle.main.loadNibNamed("TbInfoView", owner: self, options: nil)?[0] as! ZyaTbInfoView)
        tbinfoView.rankTip.text = ""
        tbinfoView.wenhao.isHidden = true
        tbinfoView.titleLab.text = "地址信息"
        return tbinfoView
        
        
    }()
    lazy var tbinfoView2: ZyaTbInfoView = {
        
        var tbinfoView = (Bundle.main.loadNibNamed("TbInfoView", owner: self, options: nil)?[0] as! ZyaTbInfoView)
        tbinfoView.rankTip.text = ""
        tbinfoView.wenhao.isHidden = true
        tbinfoView.titleLab.text = "付款信息"
        return tbinfoView
        
        
    }()
    lazy var topB:UIView = {
        let v = UIView(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 290))
        v.backgroundColor = .white
        return v
        
    }()
    
    lazy var confirmBtn:UIButton = {
        let v = UIButton(frame: CGRect(x: 20, y: 5, width: KSGlobal.screenW - 40, height: 35))
        v.layer.cornerRadius = 6
        v.layer.masksToBounds = true
        v.layer.borderWidth = 1
        v.setTitleColor(Theme.primaryColor(), for: .normal)
        v.setTitle("我已转账", for: .normal)
        v.layer.borderColor = Theme.primaryColor().cgColor
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "详情"
        topB.addSubview(tbinfoView)
        topB.addSubview(tbinfoView2)
        tbinfoView.snp.makeConstraints { (make) in
            make.left.top.right.equalTo(topB)
        }
        self.tbinfoView.contentStr = "提货日期：\(model.tjYjrq.unwrap)\n收货地址：\(model.sClientAddress.unwrap)\n备注：\(model.sRemark.unwrap)"
        
        tbinfoView2.snp.makeConstraints { (make) in
            make.top.equalTo(tbinfoView.snp.bottom)
            make.left.right.equalTo(topB)
        }
        self.tableView.tableHeaderView = topB
        
        KSNetwork.shared.doRequest(API.getOrdersDetail(idkey: model.idKey.unwrap)) { (data:BaseResult<ZyaPurchaseDetail>) in
            if data.status == "200" {
                if let infos = data.info {
                    self.details = infos
                    
                    
                    self.comfigRow()
                }
            }
        }
        
        KSNetwork.shared.doRequest(API.queryCollectionAccount(id_key: model.idKey.unwrap)) { (data:BaseResult<OperateResult>) in
            if data.flag == TRUE {
                if let infostr = data.info_object {
                    let paraph = NSMutableParagraphStyle()
                    //将行间距设置为28
                    paraph.lineSpacing = 5
                    //样式属性集合
                    let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
                                      NSAttributedString.Key.paragraphStyle: paraph]
                    
                    let conStr = "收款账户名称：\(infostr.id_no_nme.unwrap)\n收款账户账户：\(infostr.id_id_no.unwrap)\n收款金额(元)：\(infostr.min_rcv_mny_amt.unwrap)\n收款状态：\(infostr.z_pay_status.unwrap)"
                    var tipStr =  "\n请把保证金转入以上账户，再点击【我已转账】"
                    if self.model.zPayStatus.unwrap == "1" {
                        tipStr = ""
                    }
                    let sumStr = conStr + tipStr
                    
                    let sAttr = NSMutableAttributedString(string: sumStr,attributes: attributes)
                    sAttr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: (sumStr as NSString).range(of: tipStr))
                    
                    self.tbinfoView2.attributedContentStr = sAttr
                    
                    
                }
            } else {
                KSToast(data.message)
            }
        }
        
    }
    
    
    func comfigRow() {
        self.form  +++ detailSection
        for item in self.details {
            detailSection <<< ZyaTitleLabRow() {
                $0.value = ZyaTitleLabData(title: item.sGoodsNm.unwrap , left: "商品类别:\(item.sGoodsType.unwrap)\n规格:\(item.sGoodsSpec.unwrap)\n确认数量:\(item.sAmountZ.unwrap)\n确认金额:\(item.sMoneyT.unwrap)\n", right: "商品代码:\(item.sGoodsCode.unwrap)\n标包:\(item.sGoodsBz.unwrap)\n已入库数量:\(item.sExeAmount.unwrap)\n单价:\(item.sCessPrice.unwrap)\n")
            }
            
        }
        
        if self.model.zPayStatus.unwrap != "1" {
            let bottomB = UIView(frame: CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 45))
            bottomB.backgroundColor = .white
            btn = ZyaButtonPocket.shared.getCountDownBtn(id:model.idKey.unwrap)
            btn!.addTarget(self, action: #selector(preSave), for: .touchUpInside)
            bottomB.addSubview(btn!)
           
            self.tableView.tableFooterView = bottomB
        }
        
        
        
        
    }
   @objc func preSave() {
        alertUtil(self, title: "是否确认已通过转账方式将付款金额转入该界面指定账户？", operateS: [nameAction(name: "确认", action: { (_) in
            self.btn!.countdown = true
            self.saveAction()
        })])
    }
    
    // MARK: - 提交
    func saveAction() {
        
        
        let api = API.syncBankRecord(id_key: model.idKey.unwrap)
        KSNetwork.shared.doRequest(api) { (data:BaseResult<OperateResult>) in
            if data.flag == TRUE {
                KSToast(data.message.unwrap,duration: 2)
                
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: CALL_tenderinfo_REFRESH), object: nil)
                self.tableView.tableFooterView = nil
                
            } else {
                KSToast(data.message.unwrap)
            }
        }
        
    }
}
